require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#confirmOrder",
    data:{
        uid:"",
        cartId:"",
        preorderList:{},
        address:{},
        isScreen:"",
        cartIdid:"",
        state:"",
        imgSrc:"",
        number:"",
        orderInfo:{},
        userInfo:{},
        cartIdList:[],
        InvalidList:[],    //失效
        InvalidId:[],
        effectiveList:[],   //有效
        myCoupons:[],
        skuId:"",
        productId:"",
        counponId:"",
        isIntegral:"",
        offsetCoin:""
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.uid=this.searchObject&&this.searchObject.uid;
        this.cartId=this.searchObject&&this.searchObject.cartId;
        if(this.searchObject&&this.searchObject.addressId){
            this.addressId=this.searchObject&&this.searchObject.addressId;
            this.getCartId();
            this.getPreorder(4);
        }else{
            this.getCartId();
            this.getPreorder();
        }
    },
    methods: {
        //后退
        getBack: function () {
            location.href="/purchase/shoppingCart";
        },
        bgimg: function (url) {
            url = url ? url : "";
            return {
                "background-image": 'url("' + url + '"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        getCartId:function(){
            var cartIdList=this.cartId.split(",");
            var lis={};
            var arr=[];
            for(var i=0;i<cartIdList.length;i++){
                console.log(cartIdList[i]);
                lis.cartId=cartIdList[i];
                arr.push($.param(lis));

            }
            this.cartIdList=arr.join("&");
            console.log(this.cartId)
        },
        //预下单
        getPreorder:function(type,item){
            var that=this;
            var data={
                loginUid:that.uid,
                cartId:that.cartIdList
            };
            console.log("---------------")
            console.log(type)
            console.log(item)
            if(that.preorderList&&that.preorderList.list){
                console.log("11111111111111111111")
                var arr=[];
                that.preorderList.list.forEach(function(key){
                    var lis={};
                    key.products.forEach(function(val){
                        console.log("22222222222222222")
                        if(type==1&&val.skuId==that.skuId){
                            lis.skuProductCouponIdIsIntegral=that.skuId+"-"+that.productId+"-"+item.couponId+"-"+that.isIntegral;
                        }else if(val.skuId==item.skuId){
                            if(type==2){
                                if(val.isIntegral==0){
                                    lis.skuProductCouponIdIsIntegral=val.skuId+"-"+val.productId+"-"+val.couponId+"-"+1;
                                }else{
                                    lis.skuProductCouponIdIsIntegral=val.skuId+"-"+val.productId+"-"+val.couponId+"-"+0;
                                }
                            }else if(type==3){
                                if(val.couponId){
                                    lis.skuProductCouponIdIsIntegral=val.skuId+"-"+val.productId+"--"+val.isIntegral;
                                }else{
                                    return false;
                                }
                            }
                        }else{
                            lis.skuProductCouponIdIsIntegral=val.skuId+"-"+val.productId+"-"+val.couponId+"-"+val.isIntegral;
                        }
                        arr.push($.param(lis));
                        console.log("33333333333333333")
                        console.log(arr)
                    });

                });
                data.skuProductCouponIdIsIntegral=arr.join("&");
            }
            if(type==4){
                data.addressId=that.addressId;
            }
           /* if(type==1){
                // data.cartId="cartId="+that.cartId;
                data.skuProductCouponIdIsIntegral=that.skuId+"-"+that.productId+"-"+item.couponId+"-"+that.isIntegral;
            }else if(type==2){
                that.modifyInfo(item)
                // data.cartId="cartId="+that.cartId;
                if(that.isIntegral==0){
                    data.skuProductCouponIdIsIntegral=that.skuId+"-"+that.productId+"-"+that.counponId+"-"+1;
                }else{
                    data.skuProductCouponIdIsIntegral=that.skuId+"-"+that.productId+"-"+that.counponId+"-"+0;
                }
            }else if(type==3){
                if(item.couponId){
                    that.modifyInfo(item);
                    // data.cartId="cartId="+that.cartId;
                    data.skuProductCouponIdIsIntegral=that.skuId+"-"+that.productId+"--"+that.isIntegral;
                }else{
                    return false;
                }
            }else if(type==4){
                data.addressId=that.addressId;
            }*/
            console.log(data);
            that.ajax({url:"/purchase/preorder",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.preorderList=b.json.data;
                    that.address=that.preorderList.address;
                    that.getIsScreen();
                    //console.log(that.preorderList)
                }else{
                    vm.$refs.prompt.toast("预下单失败！");
                }
            });
        },
        //我的优惠券列表
        getMyCouponsByProductId:function(item){
            var that=this,data;
            that.getIsScreen(5,item);
            data={
                loginUid:that.uid,
                productId:that.productId
                // integral:0
            };
            this.ajax({url:"/purchase/getMyCouponsByProductId",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.myCoupons=b.json.data;

                }else{

                    vm.$refs.prompt.toast("获取列表失败！");
                }
            });
        },
        compareDate:function(item){
            var that=this;
            console.log(item)
            var second=getCompareDate(item.currentTime,item.limitTime,"second");
            console.log(second)
            return second;
        },
        //时分秒倒计时
        leftTimer:function(item,index,ins){
            var that=this;
            that.$nextTick(function(){
                var second=getCompareDate(item.currentTime,item.limitTime,"second");
                addTimer("data"+index+ins,second);
            });
        },
        getIsScreen:function (num,list) {
            console.log(list)
            this.isScreen=num;
            if(num==1){
                this.imgSrc=list.imgSrc;
                this.number=list.number;
                this.state=list.isStorage;
                this.cartIdid=list.cartId;
            }else if(num==5){
                this.modifyInfo(list)
            }
        },
        modifyInfo:function(list){
            // this.cartId=list.cartId;
            this.skuId=list.skuId;
            this.productId=list.productId;
            this.counponId=list.couponId;
            this.isIntegral=list.isIntegral;
        },
        //是否加入储物箱
        getCartIsStorage:function(list){
            var that=this;
            var data={
                loginUid:that.uid,
                cartId:that.cartIdid
            };
            if(list){
                this.state=list.isStorage;
                this.cartIdid=list.cartId;
            }
            console.log(list)
            if(this.state==0){
                data.state=1;
            }else{
                data.state=0;
            }
            console.log(data)
            that.ajax({url:"/purchase/cartIsStorage",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.preorderList.list.forEach(function(val){
                        val.products.forEach(function(key){
                            if(that.cartIdid==key.cartId){
                                if(key.isStorage==0){
                                    key.isStorage=1;
                                }else{
                                    key.isStorage=0;
                                }
                            }
                        });
                    });
                    if(!list){
                        that.getIsScreen();
                        vm.$refs.prompt.toast("加入储物箱成功！");
                    }

                }else{
                    vm.$refs.prompt.toast("加入储物箱失败！");
                }
            });
        },
        getSaveOrder:function(){
            var that=this;
            console.log(that.cartIdList)
            that.preorderList.list.forEach(function(key){
                key.products.forEach(function(val){
                    that.cartIdList.forEach(function(val1){
                        if(val.cartId==val1){
                            if(val.activityType!=null&&that.compareDate(val)<0){
                                console.log("ddddddddd")
                                that.InvalidList.push(val);
                                that.InvalidId.push(val1);
                            }else{
                                that.effectiveList.push(val1);
                            }
                        }
                    });

                });
            });
            if(that.InvalidList.length>0){
                that.cartId=that.InvalidId.join(",");
                that.getIsScreen(3);
            }else{
               that.saveOrder();
            }
            console.log(that.InvalidList)
            console.log(that.InvalidId)
            console.log(that.effectiveList)
        },
        //删除购物车
        deleteCartByid:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                cartId:that.InvalidId.join(",")
            };
            console.log(data)
            that.getIsScreen();
            that.ajax({url:"/purchase/deleteCartByid",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getPreorder();
                    that.saveOrder();
                }else{
                    vm.$refs.prompt.toast("移除无效商品失败！");
                }
            });
        },
        //结算订单
        saveOrder:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                addressId:that.address.addressId,
                cartId:that.cartIdList
            };
            var arr=[];
            that.preorderList.list.forEach(function(key){
                var lis={};
                key.products.forEach(function(val){
                    lis.skuProductCouponIdIsIntegral=val.skuId+"-"+val.productId+"-"+val.couponId+"-"+val.isIntegral;
                    arr.push($.param(lis));
                    if(val.offsetCoin){
                        that.offsetCoin=val.offsetCoin;
                    }
                });
                console.log(arr)
            });
            // skuProductCouponId
            // var cartIdList=this.cartId.split(",");

            // for(var i=0;i<cartIdList.length;i++){
            //     console.log(cartIdList[i]);
            //     lis.cartId=cartIdList[i];
            //     arr.push($.param(lis));
            // }
            data.skuProductCouponIdIsIntegral=arr.join("&");
            if(that.offsetCoin==0){
                vm.$refs.prompt.toast("请前往app结算支付！");
                return false;
            }
            console.log(data);
            that.ajax({url:"/purchase/saveOrder",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.orderInfo=b.json.data;
                    that.userInfo=that.orderInfo.user;
                    that.payOrder();
                    // if(that.orderInfo.allIntegral>0){
                    //     vm.$refs.prompt.toast("请前往app结算支付！");
                    //     that.getBack();
                    // }else{
                    //     //that.getIsScreen(2);
                    //     //that.payOrder();
                    // }
                    console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("结算订单失败！");
                }
            });
        },
        //支付
        payOrder:function(){
            var that=this;
            if(!that.offsetCoin){
                vm.$refs.prompt.toast("请前往app结算支付！");
                return false;
            }
            var data={
                loginUid:that.uid,
                ordersId:that.orderInfo.ordersId
            };
            console.log(data)
            that.ajax({url:"/purchase/payOrder",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    vm.$refs.prompt.toast("支付成功！");
                    location.href="/purchase/myOrder";
                }else{
                    vm.$refs.prompt.toast("结算订单失败！");
                }
            });
        },
        //跳转
        getNext:function(type){
            if(type==1){
               location.href="/address?type=3&cartId="+this.cartId;
            }
        }
    }
    ,watch:{

    }
});
var addTimer = function () {
    var list = [],
        interval;

    return function (id, time) {
        if (!interval)
            interval = setInterval(go, 1000);
        list.push({ ele: document.getElementById(id), time: time });
    };
    function go() {
        for (var i = 0; i < list.length; i++) {
            list[i].ele.innerHTML= getTimerString(list[i].time ? list[i].time -= 1 : 0);
            if (!list[i].time)
                list.splice(i--, 1);
        }
    }
    function getTimerString(time) {
        h = Math.floor(time/ 3600);
        m = Math.floor((time% 3600) / 60);
        s = Math.floor((time% 3600) % 60);
        if (time>0)
            return checkTime(h) + ":" + checkTime(m) + ":" + checkTime(s);
        else return "时间到";
    }
    function checkTime(i) {
        if (i<10) {
            i = "0" +i;
        }
        return i;
    }
} ();
//比较日前大小
function getCompareDate(startTime, endTime, diffType) {
    startTime = JSON.stringify(startTime).split("-").join("/");//replace(/-/g, '/')
    endTime = JSON.stringify(endTime).split("-").join("/");
    //将计算间隔类性字符转换为小写
    diffType = diffType.toLowerCase();
    var sTime =new Date(JSON.parse(startTime)); //开始时间
    var eTime =new Date(JSON.parse(endTime)); //结束时间
    //作为除数的数字
    var timeType =1;
    switch (diffType) {
        case"second":
            timeType =1000;
            break;
        case"minute":
            timeType =1000*60;
            break;
        case"hour":
            timeType =1000*3600;
            break;
        case"day":
            timeType =1000*3600*24;
            break;
        default:
            break;
    }
    return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType));
}